package com.java.utils;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李启旋
 * @create 2023-03-12 8:00
 */
public class SessionInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object rollback = session.getAttribute("rollback");
        if (rollback == null){
            return true;
        }
        UserIdMap.saveLists((ArrayList<List<Integer>>) rollback);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        ArrayList<List<Integer>> lists = UserIdMap.getLists();
        request.getSession().setAttribute("rollback",lists);
    }
}
